package jnpf.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.base.UserInfo;
import jnpf.config.PermissionsConfig;
import jnpf.entity.ZXGL_XiangMuJieXiangEntity;
import jnpf.entity.ZXGL_XiangMuJieXiang_JiaoFuWuEntity;
import jnpf.mapper.ZXGL_XiangMuJieXiangMapper;
import jnpf.model.zxgl_xiangmujiexiang.ZXGL_XiangMuJieXiangPagination;
import jnpf.permission.service.AuthorizeService;
import jnpf.service.ZXGL_XiangMuJieXiangService;
import jnpf.service.ZXGL_XiangMuJieXiang_JiaoFuWuService;
import jnpf.util.DateUtil;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ZXGL_XiangMuJieXiang
 * 版本： V2.0
 * 版权： 河北交投智能交通技术有限责任公司
 * 作者： 数字交投项目组
 * 日期： 2025-03-22
 */
@Service
public class ZXGL_XiangMuJieXiangServiceImpl extends ServiceImpl<ZXGL_XiangMuJieXiangMapper, ZXGL_XiangMuJieXiangEntity> implements ZXGL_XiangMuJieXiangService {


    @Autowired
    private UserProvider userProvider;

    @Resource
    PermissionsConfig permissionsConfig;
    @Autowired
    private ZXGL_XiangMuJieXiang_JiaoFuWuService zXGL_XiangMuJieXiang_JiaoFuWuService;

    @Autowired
    private AuthorizeService authorizeService;


    @Override
    public List<ZXGL_XiangMuJieXiangEntity> getList(ZXGL_XiangMuJieXiangPagination zXGL_XiangMuJieXiangPagination) {

        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_XiangMuJieXiangEntity> zXGL_XiangMuJieXiangQueryWrapper = new QueryWrapper<>();
        QueryWrapper<ZXGL_XiangMuJieXiang_JiaoFuWuEntity> zXGL_XiangMuJieXiang_JiaoFuWuQueryWrapper = new QueryWrapper<>();
        //TODO 增加权限逻辑
        UserInfo userInfo = userProvider.get();
        if (!userInfo.getIsAdministrator()) {
            String alldataRoleId = permissionsConfig.getAlldataRoleid();
            List<String> roleIds = userInfo.getRoleIds();
            if (roleIds == null || (StringUtils.isNotBlank(alldataRoleId) && !roleIds.contains(alldataRoleId))) {
                String userId = userInfo.getUserId();
                //项目结项：创建人看本人数据；主责人看自己负责项目数据；主责部门负责人看本部门负责项目数据；主责部门主管领导看所管理部门负责数据；
                zXGL_XiangMuJieXiangQueryWrapper.lambda().and(wrapper -> wrapper
                        .eq(ZXGL_XiangMuJieXiangEntity::getCreatorUserId, userId)//创建人看本人数据
                        .or()
                        .eq(ZXGL_XiangMuJieXiangEntity::getZrr, userId)//主责人看自己负责项目数据
                        .or()
                        .eq(ZXGL_XiangMuJieXiangEntity::getDepartmentManagerZzbm, userId)//主责部门负责人看本部门负责项目数据
                        .or()
                        .eq(ZXGL_XiangMuJieXiangEntity::getDepartmentHeadleaderZzbm, userId)//主责部门主管领导看所管理部门负责数据
                );
            }
        }
        int zXGL_XiangMuJieXiangNum = 0;
        if (StringUtil.isNotEmpty(zXGL_XiangMuJieXiangPagination.getZrr())) {
            zXGL_XiangMuJieXiangNum++;
            zXGL_XiangMuJieXiangQueryWrapper.lambda().eq(ZXGL_XiangMuJieXiangEntity::getZrr, zXGL_XiangMuJieXiangPagination.getZrr());
        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuJieXiangPagination.getZrrbm())) {
            zXGL_XiangMuJieXiangNum++;
            zXGL_XiangMuJieXiangQueryWrapper.lambda().eq(ZXGL_XiangMuJieXiangEntity::getZrrbm, zXGL_XiangMuJieXiangPagination.getZrrbm());
        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuJieXiangPagination.getJxrq())) {
            zXGL_XiangMuJieXiangNum++;
            List<String> JxrqList = zXGL_XiangMuJieXiangPagination.getJxrq();
            Long fir = Long.valueOf(JxrqList.get(0));
            Long sec = Long.valueOf(JxrqList.get(1));

            zXGL_XiangMuJieXiangQueryWrapper.lambda().ge(ZXGL_XiangMuJieXiangEntity::getJxrq, new Date(fir))
                    .le(ZXGL_XiangMuJieXiangEntity::getJxrq, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuJieXiangPagination.getXmmc())) {
            zXGL_XiangMuJieXiangNum++;
            zXGL_XiangMuJieXiangQueryWrapper.lambda().like(ZXGL_XiangMuJieXiangEntity::getXmmc, zXGL_XiangMuJieXiangPagination.getXmmc());
        }

        if (AllIdList.size() > 0) {
            zXGL_XiangMuJieXiangQueryWrapper.lambda().in(ZXGL_XiangMuJieXiangEntity::getId, AllIdList);
        }

        /**
         * 数据权限 此处手动写入菜单id
         */
//        String moduleId = "673059629688714885";
//        if (!userProvider.get().getIsAdministrator()) {
//            Object conditionObj = authorizeService.getCondition(zXGL_XiangMuJieXiangQueryWrapper, userProvider.get(), moduleId);
//            if (ObjectUtil.isEmpty(conditionObj)) {
//                return new ArrayList<>();
//            }
//            zXGL_XiangMuJieXiangQueryWrapper = (QueryWrapper<ZXGL_XiangMuJieXiangEntity>) authorizeService.getCondition(zXGL_XiangMuJieXiangQueryWrapper, userProvider.get(), moduleId);
//        }
        //排序
//        if (StringUtil.isEmpty(zXGL_XiangMuJieXiangPagination.getSidx())) {
//            zXGL_XiangMuJieXiangQueryWrapper.lambda().orderByDesc(ZXGL_XiangMuJieXiangEntity::getId);
//        } else {
//            try {
//                String sidx = zXGL_XiangMuJieXiangPagination.getSidx();
//                ZXGL_XiangMuJieXiangEntity zXGL_XiangMuJieXiangEntity = new ZXGL_XiangMuJieXiangEntity();
//                Field declaredField = zXGL_XiangMuJieXiangEntity.getClass().getDeclaredField(sidx);
//                declaredField.setAccessible(true);
//                String value = declaredField.getAnnotation(TableField.class).value();
//                zXGL_XiangMuJieXiangQueryWrapper = "asc".equals(zXGL_XiangMuJieXiangPagination.getSort().toLowerCase()) ? zXGL_XiangMuJieXiangQueryWrapper.orderByAsc(value) : zXGL_XiangMuJieXiangQueryWrapper.orderByDesc(value);
//            } catch (NoSuchFieldException e) {
//                e.printStackTrace();
//            }
//        }
        zXGL_XiangMuJieXiangQueryWrapper.lambda().orderByDesc(ZXGL_XiangMuJieXiangEntity::getCreatorTime);
        if ((total > 0 && AllIdList.size() > 0) || total == 0) {
            Page<ZXGL_XiangMuJieXiangEntity> page = new Page<>(zXGL_XiangMuJieXiangPagination.getCurrentPage(), zXGL_XiangMuJieXiangPagination.getPageSize());
            IPage<ZXGL_XiangMuJieXiangEntity> userIPage = this.page(page, zXGL_XiangMuJieXiangQueryWrapper);
            return zXGL_XiangMuJieXiangPagination.setData(userIPage.getRecords(), userIPage.getTotal());
        } else {
            List<ZXGL_XiangMuJieXiangEntity> list = new ArrayList();
            return zXGL_XiangMuJieXiangPagination.setData(list, list.size());
        }
    }

    @Override
    public List<ZXGL_XiangMuJieXiangEntity> getTypeList(ZXGL_XiangMuJieXiangPagination zXGL_XiangMuJieXiangPagination, String dataType) {
        String userId = userProvider.get().getUserId();
        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_XiangMuJieXiangEntity> zXGL_XiangMuJieXiangQueryWrapper = new QueryWrapper<>();
        QueryWrapper<ZXGL_XiangMuJieXiang_JiaoFuWuEntity> zXGL_XiangMuJieXiang_JiaoFuWuQueryWrapper = new QueryWrapper<>();
        int zXGL_XiangMuJieXiangNum = 0;
        if (StringUtil.isNotEmpty(zXGL_XiangMuJieXiangPagination.getZrr())) {
            zXGL_XiangMuJieXiangNum++;
            zXGL_XiangMuJieXiangQueryWrapper.lambda().eq(ZXGL_XiangMuJieXiangEntity::getZrr, zXGL_XiangMuJieXiangPagination.getZrr());
        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuJieXiangPagination.getZrrbm())) {
            zXGL_XiangMuJieXiangNum++;
            zXGL_XiangMuJieXiangQueryWrapper.lambda().eq(ZXGL_XiangMuJieXiangEntity::getZrrbm, zXGL_XiangMuJieXiangPagination.getZrrbm());
        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuJieXiangPagination.getJxrq())) {
            zXGL_XiangMuJieXiangNum++;
            List<String> JxrqList = zXGL_XiangMuJieXiangPagination.getJxrq();
            Long fir = Long.valueOf(JxrqList.get(0));
            Long sec = Long.valueOf(JxrqList.get(1));

            zXGL_XiangMuJieXiangQueryWrapper.lambda().ge(ZXGL_XiangMuJieXiangEntity::getJxrq, new Date(fir))
                    .le(ZXGL_XiangMuJieXiangEntity::getJxrq, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuJieXiangPagination.getXmmc())) {
            zXGL_XiangMuJieXiangNum++;
            zXGL_XiangMuJieXiangQueryWrapper.lambda().like(ZXGL_XiangMuJieXiangEntity::getXmmc, zXGL_XiangMuJieXiangPagination.getXmmc());
        }

        if (AllIdList.size() > 0) {
            zXGL_XiangMuJieXiangQueryWrapper.lambda().in(ZXGL_XiangMuJieXiangEntity::getId, AllIdList);
        }
        //排序
        if (StringUtil.isEmpty(zXGL_XiangMuJieXiangPagination.getSidx())) {
            zXGL_XiangMuJieXiangQueryWrapper.lambda().orderByDesc(ZXGL_XiangMuJieXiangEntity::getId);
        } else {
            try {
                String sidx = zXGL_XiangMuJieXiangPagination.getSidx();
                ZXGL_XiangMuJieXiangEntity zXGL_XiangMuJieXiangEntity = new ZXGL_XiangMuJieXiangEntity();
                Field declaredField = zXGL_XiangMuJieXiangEntity.getClass().getDeclaredField(sidx);
                declaredField.setAccessible(true);
                String value = declaredField.getAnnotation(TableField.class).value();
                zXGL_XiangMuJieXiangQueryWrapper = "asc".equals(zXGL_XiangMuJieXiangPagination.getSort().toLowerCase()) ? zXGL_XiangMuJieXiangQueryWrapper.orderByAsc(value) : zXGL_XiangMuJieXiangQueryWrapper.orderByDesc(value);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        if ("0".equals(dataType)) {
            if ((total > 0 && AllIdList.size() > 0) || total == 0) {
                Page<ZXGL_XiangMuJieXiangEntity> page = new Page<>(zXGL_XiangMuJieXiangPagination.getCurrentPage(), zXGL_XiangMuJieXiangPagination.getPageSize());
                IPage<ZXGL_XiangMuJieXiangEntity> userIPage = this.page(page, zXGL_XiangMuJieXiangQueryWrapper);
                return zXGL_XiangMuJieXiangPagination.setData(userIPage.getRecords(), userIPage.getTotal());
            } else {
                List<ZXGL_XiangMuJieXiangEntity> list = new ArrayList();
                return zXGL_XiangMuJieXiangPagination.setData(list, list.size());
            }
        } else {
            return this.list(zXGL_XiangMuJieXiangQueryWrapper);
        }
    }

    @Override
    public ZXGL_XiangMuJieXiangEntity getInfo(String id) {
        QueryWrapper<ZXGL_XiangMuJieXiangEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ZXGL_XiangMuJieXiangEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void create(ZXGL_XiangMuJieXiangEntity entity) {
        this.save(entity);
    }

    @Override
    public boolean update(String id, ZXGL_XiangMuJieXiangEntity entity) {
        entity.setId(id);
        return this.updateById(entity);
    }

    @Override
    public void delete(ZXGL_XiangMuJieXiangEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
        }
    }

    //子表方法
    @Override
    public List<ZXGL_XiangMuJieXiang_JiaoFuWuEntity> GetZXGL_XiangMuJieXiang_JiaoFuWuList(String id) {
        QueryWrapper<ZXGL_XiangMuJieXiang_JiaoFuWuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ZXGL_XiangMuJieXiang_JiaoFuWuEntity::getMainid, id);
        return zXGL_XiangMuJieXiang_JiaoFuWuService.list(queryWrapper);
    }

    //列表子表数据方法

}